package com.yanfei.zero.apis;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import me.chanjar.weixin.common.error.WxErrorException;
@RestController
@Tag(name="消息推送相关")
public class PushController  {

    @GetMapping("/pushMsg")
    @Operation(summary = "推送消息")
    public String push(@RequestParam String openid, @RequestParam String formid) {
        //1,配置公众号信息
        WxMpDefaultConfigImpl wxConfig = new WxMpDefaultConfigImpl();
        wxConfig.setAppId("wxd7db1754949797f3");
        wxConfig.setSecret("e4b0a839e2a8e5ac7a6bfbd10f000195");


        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxConfig);
        try {
            WxMpUser wxMpUser= wxMpService.getUserService().userInfo("o8YSA6KgCKcbyhrxMsL4sKIM-Bro");
            // 需要在开放平台 将微信小程序和公众号绑定
            String unionId = wxMpUser.getUnionId();
            WxMpUserList list = wxMpService.getUserService().userList();

            System.out.println(list);
        }catch (WxErrorException e){

        }




        //2，设置推送消息
        List<WxMpTemplateData> data =new ArrayList<>();
        data.add(new WxMpTemplateData("date01", "2015年01月05日"));
        data.add(new WxMpTemplateData("name01", "张三"));
        data.add(new WxMpTemplateData("amount01", "200"));
        data.add(new WxMpTemplateData("thing01", "北京"));
        WxMpTemplateMessage.MiniProgram miniProgram = new WxMpTemplateMessage.MiniProgram();
        miniProgram.setAppid("wx7869696b4bcd597f");
        miniProgram.setPagePath("pages/business/business");
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openid)//要推送的用户openid
                .templateId("MxJT_cgqaB-LFKnytz78vIPD--WFYsBLU-zLboa_q2E")//推送的模版id（在小程序后台设置）
                .url("")
                .data(data)
//                .miniProgram(miniProgram)//要跳转到小程序那个页面
                .build();
        //3，发起推送
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            System.out.println("推送失败：" + e.getMessage());
            return e.getMessage();
        }
        return "推送成功";
    }

    public static void main(String[] args) {
        new PushController().push("o8YSA6KgCKcbyhrxMsL4sKIM-Bro", "111");
    }
}
